/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package business.bd;


import java.lang.reflect.Method;
import java.util.HashMap;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import util.ServerResponse;



/**
 *
 * @author salvatore.pannozzo
 */
public class BD {
    private static Logger logger=LogManager.getLogger(BD.class);
    private String mode;
    private String className="";
    private String methodName="";
    private String packageName="";

    public String getMode() {
        return mode;
    }

    public String getClassName() {
        return className;
    }

    public String getMethodName() {
        return methodName;
    }

    public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public void setMode(String mode) {
        this.mode = mode;
        parse();
    }

    public BD(String mode) {
        this.mode = mode;
        parse();
    }
    private void parse(){
        mode=mode.trim();
        int dotPosition=mode.indexOf(".");
        if (dotPosition>0) {
            className=mode.substring(0,dotPosition);
            methodName=mode.substring(dotPosition+1,mode.length());

            className=className.substring(0,1).toUpperCase()+className.substring(1,className.length())+"BL";

        }
        /*
         else if(dotPosition==0){
            methodName=mode.substring(1,mode.length());
            String temp=new ContattoBL().getClass().getName();
            className=temp.substring(temp.lastIndexOf(".")+1,temp.length());
           // className="AbstractAssetBL";
        }
          * 
          */
    }
    public ServerResponse delegate(HashMap parameters){
        logger.info("BEGIN - delegating method for mode: "+mode);
        //aBL=new AbstractAssetBL();
        Object object=new Object();
        logger.info("Nome metodo da evocare: "+methodName);
        Class c1;
        Method m;
        try {
            c1 = Class.forName(packageName+"." + className);
            object = c1.cast(c1.newInstance());

            m=object.getClass().getMethod(methodName,new Class[]{HashMap.class});
            return (ServerResponse)m.invoke(object, parameters);

        }
        catch (Exception e) {
            logger.error("errore nel lanciare il metodo "+methodName+" con i parametri :"
                    +parameters.toString()+".\n"+e.getMessage(),e);
            logger.error("causa: ",e.getCause());

        }
        logger.info("END - delegating method "+mode);
        return new ServerResponse("", null, "KO");
    }
    public static void main(String[] args){
       
//        String m = "contatto.save";
//        BD bd = new BD(m);
//        logger.info("class name: " + bd.getClassName());
//        logger.info("method name: " + bd.getMethodName());
//        HashMap map = new HashMap();
//        map.put("nome", "pippo");
//        map.put("cognome", "baudo");
//        map.put("numTel", "3898761582");
//        map.put("id","177");
//        ContattoBL cBL= new ContattoBL();
//
//        try {
//            
//            cBL.save(map);
//
//            //ContattoBean b=new ContattoBean("1","", "pippo", "baudo", "3895544726");
//            logger.info("AAAA ");
//
//            //abl.getProvinceNazioni(map);
//        } catch (Exception e) {
//            logger.error("Errore! ", e);
//        }
    }
}
